package leetcodev1.链表;

public class LeetCode82 {

    public ListNode deleteDuplicates(ListNode head) {
        ListNode virtualNode = new ListNode(-1), pre = virtualNode;
        int num = Integer.MIN_VALUE;
        while (head != null) {
            if (head.next != null && head.val == head.next.val) {
                num = head.val;
            } else if (head.val != num) {
                pre.next = new ListNode(head.val);//避免后续的节点没有处理干净
                pre = pre.next;
            }

            head = head.next;
        }
        return virtualNode.next;
    }
}

class Answer82{

    //原地算法 但是好难写
    //站在虚拟节点看后两个
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }

        ListNode dummy = new ListNode(0, head);

        ListNode cur = dummy;
        while (cur.next != null && cur.next.next != null) {
            //虚拟节点后第一个节点 第二个节点
            if (cur.next.val == cur.next.next.val) {
                int x = cur.next.val;
                while (cur.next != null && cur.next.val == x) {
                    cur.next = cur.next.next;//删除相同元素节点
                }
            } else {
                //正常遍历不管
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}
